/*
 * Copyright 2004-2006 asoft
 * 本软件及其所有拷贝的名称，与光盘上或本软件中注明的公司同在。
 * 本软件及文档享有版权，并受国家版权法及国际协约条款的保护。
 * 您不可以从本软件中去掉其版权声明；并保证为本软件的拷贝（本部或部分）复制版权声明。
 * 您同意制止以任何形式非法拷贝本软件及文档。
 * 版权信息参见：
 *      http://www.asoft.cn/licenses
 */

package com.asoft.common.sysframe.valid;

import java.sql.Timestamp;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import com.asoft.common.sysframe.define.UserStatus;
import com.asoft.common.sysframe.manager.PostManager;
import com.asoft.common.sysframe.manager.UserManager;
import com.asoft.common.sysframe.model.Post;
import com.asoft.common.sysframe.model.User;
import com.asoft.common.util.mvc.valid.UserValidator;
import com.asoft.common.util.mvc.valid.ValidatedResult;

/**
 * <p>Title: 人员</p>
 * <p>Description: 人员 User </p>
 * <p>Copyright: Copyright (c) 2004-2006</p>
 * <p>Company: asoft</p>
 * @ $Author: amon.lei $
 * @ $Date: 2007-2-20 $
 * @ $Revision: 1.0 $
 * @ created in 2007-2-20
 *
 */

public class AddUserValidator extends UserValidator
{
        static Logger logger = Logger.getLogger(AddUserValidator.class);

        /**
         * 新增人员
         *
         * @ param: HttpServletRequest request
         * @ param: ValidatedResult vr 校验结果存放器
         * @ param: String[] params 配置参数
         */

        public void validing(HttpServletRequest request,ValidatedResult vr,String[] params)
        {
                logger.debug("开始进行新增人员前校验");
                UserManager userManager = (UserManager)this.getBean("userManager");
                
                String account = request.getParameter("account");
                if(userManager.getByAccount(account) != null)
                {
                        vr.setErrMess("account","已被使用");
                }
                this.validString(request,vr,"account",1,50);
                
                String familyName = this.validString(request,vr,"familyName",1,50);
                
                String givenName = this.validString(request,vr,"givenName",1,50);
                
                Timestamp birthday = this.validTimestamp(request,vr,"birthday","yyyy-MM-dd",false);
                
                String address = this.validString(request,vr,"address",0,150);
                
                String email = this.validString(request,vr,"email",0,50);
                
                String phone = this.validString(request,vr,"phone",0,50);

                String homeSite = this.validString(request,vr,"homeSite",0,50);

                String mobile = this.validString(request,vr,"mobile",0,50);
                
                Timestamp pwLimtDate = this.validTimestamp(request,vr,"pwLimtDate","yyyy-MM-dd",true);
                
                Timestamp idLimtDate = this.validTimestamp(request,vr,"idLimtDate","yyyy-MM-dd",true);
                
                int pwErrorNum = this.validInt(request,vr,"pwErrorNum",false);
                if(vr.isValidated("pwErrorNum"))
                {
                        if(pwErrorNum < 0)
                        {
                                vr.setErrMess("pwErrorNum","必须大于0");
                        }
                }
                
                int pwErrorLock = this.validInt(request,vr,"pwErrorLock",false);
                if(vr.isValidated("pwErrorLock"))
                {
                        if(pwErrorLock < 0)
                        {
                                vr.setErrMess("pwErrorLock","必须大于0");
                        }
                }
                
                String remark = this.validString(request,vr,"remark",0,500);
                
                String[] postIds = request.getParameterValues("postIds");
                Set posts = new LinkedHashSet();
                if(postIds != null)
                {
                        PostManager postManager = (PostManager)this.getBean("postManager");
                        for(int i = 0; i < postIds.length; i ++)
                        {
                                try
                                {
                                        Post post = (Post)postManager.get(postIds[i]);
                                        posts.add(post);
                                }
                                catch(Exception x)
                                {
                                        logger.error("(id:" + postIds[i] + ")角色已经被删除");
                                }
                        }
                }
                else
                {
                        vr.setErrMess("postIds","必须绑定岗位");
                }
                
                if(vr.isAllValidated())
                {
                        logger.debug("通过校验");
                        User user = new User();
                        user.setAccount(account);
                        user.resetPW("123456");
                        user.setPosts(posts);
                        user.setFamilyName(familyName);
                        user.setGivenName(givenName);
                        user.setBirthday(birthday);
                        user.setAddress(address);
                        user.setEmail(email);
                        user.setPhone(phone);
                        user.setHomeSite(homeSite);
                        user.setMobile(mobile);
                        user.setPwLimtDate(pwLimtDate);
                        user.setIdLimtDate(idLimtDate);
                        user.setPwErrorLock(pwErrorLock);
                        user.setPwErrorNum(pwErrorNum);
                        user.setRemark(remark);
                        user.setStatus(UserStatus.VALID);
                        request.setAttribute(User.class.getName(),user);
                }
        }
}
